#include "cell_record.h"
#include "ui_cell_record.h"

Cell_Record::Cell_Record(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Cell_Record)
{
    ui->setupUi(this);
    ui->tableView->setEditTriggers(QAbstractItemView::NoEditTriggers);
    ui->tableView->setSelectionBehavior(QAbstractItemView::SelectRows);

    initPage();
    ui->tableView->setModel(&m_model);
}

Cell_Record::~Cell_Record()
{
    delete ui;
}

void Cell_Record::initPage(QString strCondition)
{
    //查询数据库并显示
    auto l = Sql_Mgr::getInstance()->getRecord(ui->le_search->text());

    m_model.clear();
    m_model.setHorizontalHeaderLabels(QStringList{"id","用户id","图书id","开始日期","结束日期","年级","部门","权限","用户名", "密码","昵称","图书名称","价格","类型1","类型2", "类型3","数量","图片"});

    for(int i=0; i<l.size(); i++)
    {
        QList<QStandardItem*> items;
        for(int j=0; j<l[i].size(); j++)
        {
            items.append(new QStandardItem(l[i][j]));
        }
        m_model.appendRow(items);
    }
}

void Cell_Record::on_btn_clear_clicked()
{
    Sql_Mgr::getInstance()->clearRecord();
    initPage();
}


void Cell_Record::on_le_search_textChanged(const QString &arg1)
{
    initPage(arg1);
}


void Cell_Record::on_pushButton_clicked()
{
    Dlg_BookGS dlg;
    int r = ui->tableView->currentIndex().row();
    if(r<0)
    {
        return;
    }
    auto id = m_model.item(r, 2 )->text().toInt();
    dlg.setType(true);
    dlg.setBookId(id);
    int ret  = dlg.exec();
    QMessageBox::information(nullptr, "信息", ret?"归还成功":"归还失败");

    if(ret)
    {
        initPage();
    }
}

